package top.itaso.lib.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.NumberPicker;

import androidx.annotation.RequiresApi;

import java.lang.reflect.Field;

/**
 * @Description: 基于原生控件 NumberPicker 扩展自定义样式，如 字体颜色、字号、分割线 等等
 * @Author: itaso
 * @CreateDate: 2022/8/17
 */
@RequiresApi(api = Build.VERSION_CODES.M)
public class WidgetPickerNumber extends NumberPicker {
    private final String TAG = this.getClass().getSimpleName();

    public WidgetPickerNumber(Context context) {
        super(context);
        initView();
    }

    public WidgetPickerNumber(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public WidgetPickerNumber(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    @Override
    public void addView(View child) {
        super.addView(child);
        Log.d(TAG, "addView: >>>>>>child=" + child);
        updateView(child);
    }


    @Override
    public void addView(View child, ViewGroup.LayoutParams params) {
        super.addView(child, params);
        Log.d(TAG, "addView: >>>>>>child=" + child + ",params=" + params);
        updateView(child);
    }

    @Override
    public void addView(View child, int index) {
        super.addView(child, index);
        Log.d(TAG, "addView: >>>>>>child=" + child + ",index=" + index);
        updateView(child);
    }

    @Override
    public void addView(View child, int width, int height) {
        super.addView(child, width, height);
        Log.d(TAG, "addView: >>>>>>child=" + child + ",width=" + width + ",height=" + height);
        updateView(child);
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        Log.d(TAG, "addView: >>>>>>child=" + child + ",index=" + index + ",params=" + params);
        updateView(child);
    }

    public void updateFocusState(boolean hasFocus) {
        clearInputFocus();
        if (hasFocus) {
            setInputFocus();
        }

    }

    private void initView() {
        setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.e(TAG, "onFocusChange: >>>>>>initView v=" + v + ",hasFocus==" + hasFocus);
                clearInputFocus();
                if (hasFocus) {
                    setInputFocus();
                }
            }
        });
        try {
            Field mInputText = this.getField("mInputText");
            ((EditText) mInputText.get(this)).getPaint().setFakeBoldText(true);
            ((EditText) mInputText.get(this)).setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.size_text_middle));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    private void updateView(View view) {
        if (view instanceof EditText) {
            EditText edit = (EditText) view;
            edit.setFocusableInTouchMode(false);
            edit.setEnabled(false);
            //设置文字的颜色和大小
            edit.setTextColor(getResources().getColor(R.color.color_text));//Color.parseColor("#5CACEE")
            edit.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.size_text_middle));
        }

        try {
            //设置分割线大小颜色
            Field mSelectionDivider = this.getField("mSelectionDivider");
            mSelectionDivider.set(this, new ColorDrawable(getResources().getColor(R.color.transparent)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 反射获取控件 mSelectionDivider mInputText 等等进行样式修改
     *
     * @param fieldName
     * @return
     */
    private Field getField(String fieldName) {
        try {
            //设置分割线的颜色值
            Field pickerFields = NumberPicker.class.getDeclaredField(fieldName);
            pickerFields.setAccessible(true);
            return pickerFields;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private void setInputFocus() {
        setBackground(getContext().getDrawable(R.drawable.shape_bg_stroke_hl));
        Field mInputText = this.getField("mInputText");
        try {
            ((EditText) mInputText.get(this)).getPaint().setFakeBoldText(true);
            ((EditText) mInputText.get(this)).setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.size_text_middle));
            ((EditText) mInputText.get(this)).setTextColor(getContext().getColor(R.color.color_text_hl));
            ((EditText) mInputText.get(this)).setCursorVisible(false);
            ((EditText) mInputText.get(this)).requestFocus();
            ((EditText) mInputText.get(this)).requestFocusFromTouch();
            ((EditText) mInputText.get(this)).setFocusable(true);
            ((EditText) mInputText.get(this)).setVisibility(View.VISIBLE);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    private void clearInputFocus() {
        Field mInputText = this.getField("mInputText");
        setBackground(null);
        try {
            ((EditText) mInputText.get(this)).clearFocus();
            ((EditText) mInputText.get(this)).setTextColor(getContext().getColor(R.color.color_text));
            ((EditText) mInputText.get(this)).setFocusable(false);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

}
